﻿Imports Data


Public Class MarcaForm
    Private _id As Integer

    Private Enum Interfaz_Modificar
        Ingresar = 1
        Modificar = 2
    End Enum


    Private Sub MarcaForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            Me.CargarMarcaListView()
        Catch ex As Exception
            Debug.Print("Error en la carga de listview")
        End Try
    End Sub

    Private Function CargarMarcaListView() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.lvMarcas.Items.Clear()
                Dim lista As New List(Of Marca)
                lista = db.Marca.Select(Function(p) p).ToList
                For Each item In lista
                    Me.lvMarcas.Items.Add(New ListViewItem(New String() {item.id, item.nombre}) With {.Name = item.id})
                Next
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub IngresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IngresarToolStripMenuItem.Click
        Try
            Me.ModificarInterfazIngreso()
        Catch ex As Exception

        End Try
    End Sub

    Private Function ModificarInterfazIngreso() As Boolean
        Try
            Me.txtNombre.Text = String.Empty
            Me.btnIngresar.Text = "Ingresar"
            Me.txtNombre.Focus()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub ModificarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Try
            Me.ModificarInterfazModificacion()
        Catch ex As Exception

        End Try
    End Sub

    Private Function ModificarInterfazModificacion() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.txtNombre.Text = String.Empty
                Me.btnIngresar.Text = "Modificar"
                Me._id = Me.lvMarcas.SelectedItems(0).Name
                Me.txtNombre.Text = db.Marca.Where(Function(p) p.id = Me.lvMarcas.SelectedItems(0).Name).Select(Function(p) p.nombre).FirstOrDefault

            End Using
           

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnIngresar_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        Try
            Dim nuevaMarca As New Marca
            nuevaMarca.nombre = Me.txtNombre.Text
            If (Me.btnIngresar.Text = "Modificar") Then
                Me.ModificarMarca(nuevaMarca)
                Me.LimpiarInterfaz()
                Me.CargarMarcaListView()
            Else
                Me.IngresarMarca(nuevaMarca)
                Me.LimpiarInterfaz()
                Me.CargarMarcaListView()
            End If
        Catch ex As Exception

        End Try
    End Sub


   
    Private Function ModificarMarca(ByVal nuevaMarca As Marca) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim marca As Marca = db.Marca.Select(Function(p) p).Where(Function(p) p.id = _id).FirstOrDefault
                marca.nombre = nuevaMarca.nombre
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function IngresarMarca(ByVal marca As Marca) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                db.Marca.InsertOnSubmit(marca)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function



    Private Function LimpiarInterfaz() As Boolean
        Try
            Me.txtNombre.Text = String.Empty
            Me.btnIngresar.Text = "Ingresar"
            Me.txtNombre.Focus()

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    
    Private Sub EliminarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Try
            If (MsgBox("Esta seguro de eliminar este registro", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok) Then
                If (Me.EliminarMarcas(Me.lvMarcas.SelectedItems(0).Name)) Then
                    MsgBox("Se elimino satisfactoriamente")
                    Me.LimpiarInterfaz()
                    Me.CargarMarcaListView()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function EliminarMarcas(ByVal id As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim registro As Marca = db.Marca.Select(Function(p) p).Where(Function(p) p.id = id).FirstOrDefault
                db.Marca.DeleteOnSubmit(registro)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function



    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Try
            Me.Close()

        Catch ex As Exception

        End Try
    End Sub
End Class